/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import database.DBConnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.PropertiesManager;

/**
 *
 * @author rosana
 */
public class PacienteDAO implements OperacoesDAO {
    
     private Connection conexao;

    public PacienteDAO() throws ClassNotFoundException, SQLException {
        conexao = DBConnection.getConnection();

    }

     @Override
     public void inserir(Object obj) throws SQLException {
         String SQL = buscaSQL("insert.Paciente");
        if (obj instanceof Paciente) {
            Paciente pac = (Paciente) obj;
            PreparedStatement st = conexao.prepareStatement(SQL);
            st.setInt(1, pac.getNumPasta());
            st.setString(2, pac.getNomeResp());
            st.setString(3, pac.getDiagnostico());
            st.setInt(4, pac.getConvenio().getIdConvenio());
            st.execute();
        }
    }

   @Override
    public boolean excluir(Object obj) throws SQLException {
        Integer num = (Integer) obj;
        int id = num.intValue();
        String sql = (String) buscaSQL("delete.Paciente");
        PreparedStatement stmt = conexao.prepareStatement(sql);
        stmt.setInt(1, id);
        stmt.execute();
        stmt.close();
        return true;
    }

    
    
     @Override
    public void editar(Object obj) throws SQLException {
        Paciente pac = (Paciente) obj;
        String sql = (String) buscaSQL("Update.Paciente");
        PreparedStatement stmt = conexao.prepareStatement(sql);
        stmt.setInt(1, pac.getNumPasta());
        stmt.setString(2, pac.getNomeResp());
        stmt.setString(3, pac.getDiagnostico());
        stmt.setInt(4, pac.getConvenio().getIdConvenio());
        stmt.setInt(5, pac.getPessoa().getIdPessoa());
        stmt.execute();
        stmt.close();
        //return true;
    }

    @Override
    public ArrayList pesquisar() throws SQLException {
        ArrayList retorno = new ArrayList();
        String SQL = buscaSQL("selectComboPacienteCompromisso");
        Statement st = conexao.createStatement();
        ResultSet res = st.executeQuery(SQL);
        while (res.next()) {
            int idPessoa = res.getInt(1);
            String nome = res.getString(2); 
            Pessoa pes = new Pessoa(idPessoa, nome, null, null, null, null, null, null, null);
            Paciente pac = new Paciente(pes, 0, null, null, null, null, null, null, null, null, null, null, null);
            retorno.add(pac);
        }
        return retorno;
    }
    
    public ArrayList pesquisarPacientes() throws SQLException {
        ArrayList retorno = new ArrayList();
        String SQL = buscaSQL("selectPesquisaPaciente");
        Statement st = conexao.createStatement();
        ResultSet res = st.executeQuery(SQL);
        while (res.next()) {
            int idpac = res.getInt(1);
            String nome = res.getString(2);
            String cpf = res.getString(3);
            String rg = res.getString(4);
             String a = res.getString(5).substring(0, 4);
            String b = res.getString(5).substring(5, 7);
            String c = res.getString(5).substring(8, 10);
            String datanasc = (c+"/"+b+"/"+a);
            String endereco = res.getString(6);
            String telefone = res.getString(7);
            String celular = res.getString(8);
            String email = res.getString(9);
            int numpasta = res.getInt(11);
            String nomeResp = res.getString(12);
            String diagnostico = res.getString(13);
            int idConvenio = res.getInt(14);
            String NomeConvenio = res.getString(15);
            Convenio con = new Convenio(idConvenio, NomeConvenio);
            Pessoa pes = new Pessoa(idpac, nome, cpf, rg, datanasc, endereco, telefone, celular, email);
            Paciente pac = new Paciente(pes, numpasta, nomeResp, diagnostico, con, nome, cpf, rg, datanasc, endereco, telefone, celular, email);
            retorno.add(pac);
        }
        return retorno;
    }
    
    
    
    
    
    
public Paciente pesquisarById(String chave) throws SQLException {
        Paciente user = null;
        String SQL = buscaSQL("selectByIdPaciente");
        PreparedStatement st = conexao.prepareStatement(SQL);
        st.setString(1, chave);
        ResultSet res = st.executeQuery();
        while (res.next()) {
            int idpac = res.getInt(1);
            String nome = res.getString(2);
            String cpf = res.getString(3);
            String rg = res.getString(4);
            String a = res.getString(5).substring(0, 4);
            String b = res.getString(5).substring(5, 7);
            String c = res.getString(5).substring(8, 10);
            String datanasc = (c+"/"+b+"/"+a);
            String endereco = res.getString(6);
            String telefone = res.getString(7);
            String celular = res.getString(8);
            String email = res.getString(9);
            int numpasta = res.getInt(11);
            String nomeResp = res.getString(12);
            String diagnostico = res.getString(13);
            int idConvenio = res.getInt(14);
            String NomeConvenio = res.getString(15);
            Convenio con = new Convenio(idConvenio, NomeConvenio);
            Pessoa pes = new Pessoa(idpac, nome, cpf, rg, datanasc, endereco, telefone, celular, email);
            user = new Paciente(pes, numpasta, nomeResp, diagnostico, con, nome, cpf, rg, datanasc, endereco, telefone, celular, email);
        }
        return user;
    }
     
     private String buscaSQL(String chave) {
        String sql = null;
        try {
            PropertiesManager pm = new PropertiesManager("sql.properties");
            HashMap dados = pm.readPropertiesFile();
            sql = (String) dados.get(chave);
        } catch (IOException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sql;
    }
    
}
